package com.fms.controller;

import com.fms.annotation.HandleSensitiveMessage;
import com.fms.dto.JsonRequest;
import com.fms.dto.ProfileUpdateQuery;
import com.fms.dto.SysUserAdjustQuery;
import com.fms.dto.SysUserImageUpdateDto;
import com.fms.entity.SysUser;
import com.fms.exception.CheckException;
import com.fms.result.ResultData;
import com.fms.security.CheckUtils;
import com.fms.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
@RequiredArgsConstructor
public class SysUserController {

    private final SysUserService sysUserService;

    private final CheckUtils checkUtils;
    //获取员工信息
    @GetMapping("/getUsers/{username}")
    public ResultData getUser(@PathVariable("username") String username){
        return ResultData.success(sysUserService.getUserInfo(username));
    }

    //获取员工列表
    @GetMapping("/getAllUsers")
    public ResultData getAllUsers(){
        List<SysUser> sysUsers = sysUserService.getAllUsers();
        return ResultData.success(sysUsers);
    }

    @PostMapping("/updateImg")
    public ResultData updateImg(@RequestBody SysUserImageUpdateDto sysUserImageUpdateDto){
        System.out.println(sysUserImageUpdateDto);
        return sysUserService.updateImg(sysUserImageUpdateDto);
    }

    //更新员工信息(useless)
    @PostMapping("/updateProfile")
    public ResultData updateProfile(@RequestBody ProfileUpdateQuery profileUpdateQuery){
        String info = sysUserService.updateProfile(profileUpdateQuery);
        return ResultData.success(info);
    }

    //更新员工权限
    @PostMapping("/adjustSysUser")
    public ResultData adjustSysUser(@RequestBody SysUserAdjustQuery sysUserAdjustQuery){
        System.out.println(sysUserAdjustQuery);
        String info = sysUserService.adjustSysUser(sysUserAdjustQuery);
        return ResultData.success(info);
    }

    //新增员工
    @HandleSensitiveMessage
    @PostMapping("/addSysUser")
    public ResultData addNewSysUser(@RequestBody SysUser sysUser){
        checkUtils.checkUsernameSyntax(sysUser.getUsername(),true);
        return ResultData.success( sysUserService.addNewSysUser(sysUser));
    }

    //删除员工
    @PostMapping("/deleteSysUser")
    public ResultData deleteSysUser(@RequestBody SysUser[] sysUsers){
        System.out.println(sysUsers.length);
        String s = sysUserService.deleteSysUser(sysUsers);
        return ResultData.success(s);
    }

    //管理员指定密码
    @PostMapping("/alterSysUserPassword")
    public ResultData alterSysUserPassword(@RequestBody SysUser sysUser){
        return sysUserService.alterSysUserPassword(sysUser);
    }

    /**
     * 修改密码
     * @param passwordRequest 密码JSON
     * @return ResultData
     * @throws CheckException 密码格式or匹配错误
     */
    @PostMapping("/updatePassword")
    public ResultData updatePassword(@RequestBody JsonRequest.PasswordRequest passwordRequest){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        String newPassword = passwordRequest.getNewPassword();
        String oldPassword=passwordRequest.getOldPassword();
        checkUtils.checkPasswordSyntax(newPassword);
        checkUtils.checkPasswordSyntax(oldPassword);
        checkUtils.checkPasswordMatcher(oldPassword, username);
        return ResultData.success(sysUserService.updatePassword(username,oldPassword,newPassword));
    }

    /**
     * 登录后校验的空接口
     * @return ResultData
     */
    @PostMapping("/auth")
    public ResultData auth(){
        return ResultData.success("success");
    }


}
